﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace Historia.Controls
{
    /// <summary>
    /// Interaction logic for ctrTexte.xaml
    /// </summary>
    public partial class ctrTexte : UserControl
    {
        #region Membres
        protected TreeViewItem _chapitre = null;
        protected TreeViewItem _scene = null;
        #endregion

        #region Constructeur

        public ctrTexte()
        {
            InitializeComponent();
        }

        #endregion

        #region Propriétés

        public TreeViewItem Chapitre {
            get { return this._chapitre; }
            set{ 
                this._chapitre = value;
                this.AfficherChapitre();
            }
        }

        public TreeViewItem Scene {
            get { return this._scene; }
            set { 
                this._scene = value;
                this.AfficherScene();
            }
        }
        #endregion

        #region Méthodes

        protected void AfficherChapitre() {
            if (this.Chapitre != null)
            {
                DataRow drChap = (DataRow)this.Chapitre.Tag;

                StackPanel stack = new StackPanel();
                stack.Orientation = Orientation.Horizontal;

                Label lblNo = new Label();
                lblNo.Content = (drChap.Field<Int16>("ordre")+1).ToString() + ".";
                lblNo.VerticalAlignment = System.Windows.VerticalAlignment.Center;

                TextBox txtNomChapitre = new TextBox();
                txtNomChapitre.BorderBrush = null;
                txtNomChapitre.MaxLength = 255;
                txtNomChapitre.Margin = new Thickness(0, 2, 0, 0);
                txtNomChapitre.Text = drChap.Field<string>("nom");
                txtNomChapitre.TextChanged += new TextChangedEventHandler(txtNomChapitre_TextChanged);

                stack.Children.Add(lblNo);
                stack.Children.Add(txtNomChapitre);

                this.expChapitre.Header = stack;

                this.txtResumeChapitre.Text = drChap.Field<string>("resume");
                this.Visibility = System.Windows.Visibility.Visible;
            }
            else
            {
                this.txtResumeChapitre.Text = "";
                this.Visibility = System.Windows.Visibility.Hidden;
            }
        }

        

        public void AfficherScene()
        {
            if (this.Scene != null)
            {
                DataRow drScene = (DataRow)this.Scene.Tag;
                DataRow drChap = (DataRow)this.Chapitre.Tag;

                StackPanel stack = new StackPanel();
                stack.Orientation = Orientation.Horizontal;

                Label lblNo = new Label();
                lblNo.Content = (drChap.Field<Int16>("ordre") + 1).ToString() + "." + (drScene.Field<Int16>("ordre") + 1).ToString();
                lblNo.VerticalAlignment = System.Windows.VerticalAlignment.Center;

                TextBox txtNomScene = new TextBox();
                txtNomScene.BorderBrush = null;
                txtNomScene.MaxLength = 255;
                txtNomScene.Margin = new Thickness(0, 2, 0, 0);
                txtNomScene.Text = drScene.Field<string>("nom");
                txtNomScene.TextChanged += new TextChangedEventHandler(txtNomScene_TextChanged);

                stack.Children.Add(lblNo);
                stack.Children.Add(txtNomScene);

                this.expScene.Header = stack;

                //Fils
                this.txtFils.Text = "";
                foreach (DataRow drFilScene in drScene.GetChildRows("FK_Scenes_Fils_Scenes"))
                {
                    DataRow drFil = drFilScene.GetParentRow("FK_Fils_Fils_Scenes");
                    this.txtFils.Text += drFil.Field<string>("abreviation") + ", ";

                }
                //Personnages
                this.txtPersonnages.Text = "";
                foreach (DataRow drPersonnageScene in drScene.GetChildRows("FK_Scenes_Personnages_Scenes"))
                {
                    DataRow drPersonnage = drPersonnageScene.GetParentRow("FK_Personnages_Personnages_Scenes");
                   this.txtPersonnages.Text += drPersonnage.Field<string>("abreviation") + ", ";
                 
                }
                //Lieux
                this.txtLieux.Text = "";
                foreach (DataRow drLieuScene in drScene.GetChildRows("FK_Scenes_Lieux_Scenes"))
                {
                    DataRow drLieu = drLieuScene.GetParentRow("FK_Lieux_Lieux_Scenes");
                    this.txtLieux.Text += drLieu.Field<string>("nom") + ", ";

                }
                //Items
                this.txtItems.Text = "";
                foreach (DataRow drItemScene in drScene.GetChildRows("FK_Scenes_Items_Scenes"))
                {
                    DataRow drItem = drItemScene.GetParentRow("FK_Items_Items_Scenes");
                    this.txtItems.Text += drItem.Field<string>("nom") + ", ";

                }
                
                //Date

                if (drScene.Field<Int16?>("annee").HasValue)
                {
                    this.txtAnnee.Text = drScene.Field<Int16?>("annee").ToString();
                }
                else
                {
                    this.txtAnnee.Text = "";
                }

                if (drScene.Field<Int16?>("mois").HasValue)
                {
                    this.txtMois.Text = drScene.Field<Int16?>("mois").ToString();
                }
                else
                {
                    this.txtMois.Text = "";
                }

                if (drScene.Field<Int16?>("jour").HasValue)
                {
                    this.txtJour.Text = drScene.Field<Int16?>("jour").ToString();
                }
                else
                {
                    this.txtJour.Text = "";
                }

                if (drScene.Field<DateTime?>("heure").HasValue)
                {
                    this.txtHeure.Text = drScene.Field<DateTime?>("heure").Value.Hour.ToString("00");
                    this.txtMinute.Text = drScene.Field<DateTime?>("heure").Value.Minute.ToString("00");
                    this.txtSeconde.Text = drScene.Field<DateTime?>("heure").Value.Second.ToString("00");
                }
                else
                {
                    this.txtHeure.Text = "";
                    this.txtMinute.Text = "";
                    this.txtSeconde.Text = "";
                }
                

                this.txtResumeScene.Text = drScene.Field<string>("resume");
                this.txtTexte.Text = drScene.Field<string>("texte");
                this.expScene.Visibility = System.Windows.Visibility.Visible;
                this.txtTexte.Visibility = System.Windows.Visibility.Visible;
            }
            else
            {
                this.txtFils.Text = "";
                this.txtPersonnages.Text = "";
                this.txtLieux.Text = "";
                this.txtItems.Text = "";
                this.txtResumeScene.Text = "";
                this.txtTexte.Text = "";
                this.expScene.Visibility = System.Windows.Visibility.Hidden;
                this.txtTexte.Visibility = System.Windows.Visibility.Hidden;
            }
        }
        #endregion

        private void txtResumeChapitre_TextChanged(object sender, TextChangedEventArgs e)
        {
            if (this.Chapitre != null)
            {
                DataRow dr = (DataRow)this.Chapitre.Tag;
                dr.SetField<string>("resume", this.txtResumeChapitre.Text);
            }
        }

        private void txtResumeScene_TextChanged(object sender, TextChangedEventArgs e)
        {
            if (this.Scene != null)
            {
                DataRow dr = (DataRow)this.Scene.Tag;
                dr.SetField<string>("resume", this.txtResumeScene.Text);
            }
        }

        private void txtTexte_TextChanged(object sender, TextChangedEventArgs e)
        {
            if (this.Scene != null)
            {
                DataRow dr = (DataRow)this.Scene.Tag;
                dr.SetField<string>("texte", this.txtTexte.Text);
            }
        }

        void txtNomChapitre_TextChanged(object sender, TextChangedEventArgs e)
        {
            if (this.Chapitre != null)
            {
                DataRow dr = (DataRow)this.Chapitre.Tag;
                dr.SetField<string>("nom", ((TextBox)sender).Text);
                ((TextBlock)((StackPanel)this.Chapitre.Header).Children[2]).Text = ((TextBox)sender).Text;
            }
        }

        void txtNomScene_TextChanged(object sender, TextChangedEventArgs e)
        {
            if (this.Scene != null)
            {
                DataRow dr = (DataRow)this.Scene.Tag;
                dr.SetField<string>("nom", ((TextBox)sender).Text);
                ((TextBlock)((StackPanel)this.Scene.Header).Children[2]).Text = ((TextBox)sender).Text;
            }
        }

        private void txtAnnee_LostFocus(object sender, RoutedEventArgs e)
        {
            if (this.Scene != null)
            {
                try
                {
                    DataRow dr = (DataRow)this.Scene.Tag;
                    if ((this.txtAnnee.Text.Length > 0))
                    {
                        if ((Convert.ToInt16(this.txtAnnee.Text) >= 0) && (Convert.ToInt16(this.txtAnnee.Text) <= 9999))
                        {
                            dr.SetField<Int16?>("annee", Convert.ToInt16(this.txtAnnee.Text));
                        }
                        else
                        {
                            MessageBox.Show("L'année est invalide. Elle doit être comprise entre 0 et 9999.", "Saisie Invalide", MessageBoxButton.OK, MessageBoxImage.Warning);
                            this.txtAnnee.Text = "";
                            dr.SetField<Int16?>("annee", null);
                        }
                    
                    }
                    else
                    {
                       dr.SetField<Int16?>("annee", null);
                    }
                }
                catch (Exception)
                {

                    MessageBox.Show("L'année est invalide. Elle doit être comprise entre 0 et 9999.", "Saisie Invalide", MessageBoxButton.OK, MessageBoxImage.Warning);
                    this.txtAnnee.Text = "";
                }

            }
        }

        private void txtMois_LostFocus(object sender, RoutedEventArgs e)
        {
            if (this.Scene != null)
            {
                try
                {
                    DataRow dr = (DataRow)this.Scene.Tag;
                    if ((this.txtMois.Text.Length > 0))
                    {
                        if ((Convert.ToInt16(this.txtMois.Text) >= 1) && (Convert.ToInt16(this.txtMois.Text) <= 12))
                        {
                            dr.SetField<Int16?>("mois", Convert.ToInt16(this.txtMois.Text));
                        }
                        else
                        {
                            MessageBox.Show("Le mois est invalide. il doit être comprise entre 1 et 12.", "Saisie Invalide", MessageBoxButton.OK, MessageBoxImage.Warning);
                            dr.SetField<Int16?>("mois", null);
                            this.txtMois.Text = "";
                        }
                    
                            
                    }
                    else
                    {
                        dr.SetField<Int16?>("mois", null);
                    }

                }
                catch (Exception)
                {

                    MessageBox.Show("Le mois est invalide. il doit être comprise entre 1 et 12.", "Saisie Invalide", MessageBoxButton.OK, MessageBoxImage.Warning);
                    this.txtMois.Text = "";
                }
             }
        }

        private void txtJour_LostFocus(object sender, RoutedEventArgs e)
        {
            if (this.Scene != null)
            {
                try
                {
                    DataRow dr = (DataRow)this.Scene.Tag;
                    if ((this.txtJour.Text.Length > 0))
                    {
                        if ((Convert.ToInt16(this.txtJour.Text) >= 1) && (Convert.ToInt16(this.txtJour.Text) <= 31))
                        {
                            dr.SetField<Int16?>("jour", Convert.ToInt16(this.txtJour.Text));
                        }
                        else
                        {
                            MessageBox.Show("Le jour est invalide. il doit être comprise entre 1 et 31.", "Saisie Invalide", MessageBoxButton.OK, MessageBoxImage.Warning);
                            this.txtJour.Text = "";
                            dr.SetField<Int16?>("jour", null);
                        }

                        
                    }
                    else
                    {
                        dr.SetField<Int16?>("jour", null);
                    }

                }
                catch (Exception)
                {

                    MessageBox.Show("Le jour est invalide. il doit être comprise entre 1 et 31.", "Saisie Invalide", MessageBoxButton.OK, MessageBoxImage.Warning);
                    this.txtJour.Text = "";
                }
            }
        }

        private void txtHeure_LostFocus(object sender, RoutedEventArgs e)
        {
            if (this.Scene != null)
            {
                try
                {
                    DataRow dr = (DataRow)this.Scene.Tag;
                    if ((this.txtHeure.Text.Length > 0) && (this.txtMinute.Text.Length > 0) && (this.txtSeconde.Text.Length > 0))
                    {
                        if ((Convert.ToInt32(this.txtHeure.Text) >= 0) && (Convert.ToInt32(this.txtHeure.Text) <= 23)
                            && (Convert.ToInt32(this.txtMinute.Text) >= 0) && (Convert.ToInt32(this.txtMinute.Text) <= 59)
                            && (Convert.ToInt32(this.txtSeconde.Text) >= 0) && (Convert.ToInt32(this.txtSeconde.Text) <= 59))
                        {
                            dr.SetField<DateTime>("heure", new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, Convert.ToInt32(this.txtHeure.Text), Convert.ToInt32(this.txtMinute.Text), Convert.ToInt32(this.txtSeconde.Text)));
                        }
                        else
                        {
                            MessageBox.Show("L'heure est invalide.", "Saisie Invalide", MessageBoxButton.OK, MessageBoxImage.Warning);
                        }

                    }
                    else
                    {
                        dr.SetField<DateTime?>("heure", null);
                    }

                }
                catch (Exception ex)
                {

                    MessageBox.Show("L'heure est invalide..", "Saisie Invalide", MessageBoxButton.OK, MessageBoxImage.Warning);
                }
            }
        }
    }
}
